package com.earthea.chatbot.login.controller;

import com.earthea.chatbot.common.entity.BaseRsp;
import com.earthea.chatbot.domain.LoginUser;
import com.earthea.chatbot.login.common.util.SecurityUtils;
import com.earthea.chatbot.login.entity.request.LoginReq;
import com.earthea.chatbot.login.entity.request.RegisterReq;
import com.earthea.chatbot.login.entity.response.LoginInfoRsp;
import com.earthea.chatbot.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public BaseRsp register(@RequestBody @Validated RegisterReq user) {
        return userService.register(user);
    }

    /**
     * 登录方法
     *
     * @param loginReq 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public BaseRsp login(@RequestBody @Validated LoginReq loginReq) {
        return userService.login(loginReq);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public BaseRsp getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        LoginInfoRsp response = BaseRsp.success(LoginInfoRsp.class);
        response.setUser(loginUser);
        return response;
    }

}
